package com.bobi.tfca.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.nio.charset.Charset;

public class HtmltoPdfUtil {

    /**
     * 获取当前根目录
     * @return rootpath路径
     */
    public static String getRootPath() {
        //获取跟目录
        File file;
        String path = null;
        try {
            file = new File(ResourceUtils.getURL("classpath:").getPath());
            if(!file.exists()) {
                file = new File("");
            }
            path = file.getAbsolutePath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return path;
    }

    /**
     * 字体
     */
    public static class AsianFontProvider extends XMLWorkerFontProvider {
        @Override
        public Font getFont(final String fontname, String encoding, float size, final int style) {
            String fntname = fontname;
            if (fntname == null) {
                /*使用的windows里的宋体，可将其文件放资源文件中引入
                 *请确保simsun.ttc字体在windows下支持
                 *我是将simsun.ttc字体打进maven的jar包中使用
                 */
                fntname = "fonts/simsun.ttc";
            }
            if (size == 0) {
                size = 4;
            }
            return super.getFont(fntname, encoding, size, style);
        }
    }

    /**
     * 通过html的字符串生成pdf可保存在本地
     * @param htmlString html字符串
     * @param pdfPath 想要生成pdf的目标路径
     */
    public static void toPdf(String htmlString, String pdfPath) {
        try {
            Document document = new Document(PageSize.A4);
            PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
            document.open();
            document.addAuthor("pdf作者");
            document.addCreator("pdf创建者");
            document.addSubject("pdf主题");
            document.addCreationDate();
            document.addTitle("pdf标题,可在html中指定title");
            XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
            InputStream inputStream = null;
            worker.parseXHtml(pdfWriter, document, new ByteArrayInputStream(htmlString.getBytes("UTF-8")), inputStream, Charset.forName("UTF-8"), new AsianFontProvider());
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 生成临时pdf 方便转发
     * @param htmlString html字符串
     * @return 二进制
     */
    public static byte[] toPdfWithBit(String htmlString){
        Document document =null;
        ByteArrayOutputStream baos = null;
        try{
            document = new Document(PageSize.A4);
            baos = new ByteArrayOutputStream();//构建字节输出流
            PdfWriter pdfWriter=PdfWriter.getInstance(document,baos);//将PDF文档对象写入到流
            document.open();
            document.addAuthor("pdf作者");
            document.addSubject("pdf主题");
            document.addCreator("pdf创建者");
            document.addCreationDate();
            document.addTitle("pdf标题,可在html中指定title");
            XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
            InputStream inputStream = null;
            worker.parseXHtml(pdfWriter, document, new ByteArrayInputStream(htmlString.getBytes("UTF-8")), inputStream, Charset.forName("UTF-8"), new AsianFontProvider());
            document.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        return  baos.toByteArray();
    }
}
